<%@page contentType="text/html" pageEncoding="UTF-8"
        import="java.util.List,
        java.util.ArrayList,
        gandalf.model.*,
        gandalf.controller.*"
        %>
<!DOCTYPE HTML>

<html>
    <head>
        <meta charset="utf-8">
        <title>Prodaja Kart - Gandalf</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <script src="javascript/jquery-1.4.2.min.js"></script>
        <script>

            $(function() {
                var text;
                $("#kategorije li a:not(.current)").click(function() {
                    $("#kategorije li a.current").removeClass("current");
                    $(this).addClass("current");
                    text = $(this).text();

                    if(text == "Vse kategorije") {
                        $("#dogodki li").slideDown(200);
                        return false;
                    }

                    
                    $("#dogodki li:not(." + text + ")").fadeOut(200, function() {
                        $("#dogodki li").each(function() {

                            if($(this).hasClass(text)) {
                                $(this).slideDown(200);
                            }
                        });
                    });
                });
            });
        </script>
    </head>

    <%
                DogodekDao dogodekDao = new DogodekDao();
                CartJpaController cartDao = new CartJpaController();
                StrankaJpaController strankaDao = new StrankaJpaController();

                List<Dogodek> dogodki = new ArrayList<Dogodek>();
                
                dogodki = dogodekDao.all(false);
                System.out.println("\n\n\n\nvelikost: " + dogodki.size() +" \n\n");
                Long idStranke = (Long) request.getSession().getAttribute("stranka_id");
                String idPostavke =request.getParameter("bPostavko");
                System.out.println("id postavke ki jo bo birsalo: " + idPostavke);

                Stranka stranka = null;
                if (idStranke != null) {
                    stranka = strankaDao.findStranka(idStranke);
                }


                Long cartId = (Long) request.getSession().getAttribute("kosarica_id");
                Cart cart = null;
                if (cartId != null) {
                    cart = cartDao.findCart(cartId);
                    if(idPostavke!=null && idPostavke!=""){
                        System.out.println("biris postavko: " + idPostavke + "\n");
                        cart.odstraniPostavko(Long.parseLong(idPostavke));
                        cartDao.edit(cart);
                    }
                }

    %>
    <body>

        <div id="header">
            <h1 id="logo"><a href="index.jsp">Gandalf</a></h1>
            <ul id="nav">
                <li><a href="index.jsp">Dogodki</a></li>
                <li><a href="mailto:gandalf_support@gmail.com">Kontakt</a></li>
                <li><a href="<%= stranka!=null ? "logout.jsp" : "login.jsp" %>">
                        <%= stranka!=null ? "Odjava" : "Prijava" %></a></li>
            </ul>
        </div><!-- header -->
        <div id="section">
            <%
                        String notice = (String) request.getSession().getAttribute("notice");
                        if (notice != null) {
                            request.getSession().setAttribute("notice", null);
            %>
            <p class="notice">
                <%= notice%>
            </p>
            <% } notice= null;%>
            <ul id="kategorije">
                <li>
                    <a href="#">Vse kategorije</a>
                </li>
                <% for (String kategorija : dogodekDao.getKategorije(dogodki)) {%>
                <li>
                    <a href="#"><%= kategorija%></a>
                </li>
                <% }%>
            </ul>

            <ul id="dogodki">
                <% for (Dogodek d : dogodki) {%>
                <li class="<%= d.getKategorija()%>">
                    <h2><a href="showDogodek.jsp?id=<%= d.getId()%>"><%= d.getNaziv()%></a></h2>
                    <div>
                        <p class="description">
                            <strong><%= d.getLokacija().getNaziv()%></strong><br>
                            <%= d.getLokacija().getKraj() + ", " + d.getLokacija().getDrzava()%>
                        </p>
                        <div class="action">
                            <a href="showDogodek.jsp?id=<%= d.getId()%>#vstopnice" class="basket"><span class="plus">+</span> Dodaj v košarico</a>
                        </div>
                    </div>
                </li>
                <% }%>

            </ul>
            <div id="cart">
                 <% if (stranka != null) {%>
                <p class="stranka">Prijavljeni ste kot <a href="profile.jsp" class="Prijavljen"> <%= stranka.getIme() + " " + stranka.getPriimek()%></a>.
                    <%if (idStranke==2){ %><a href="vmesnik.jsp">(Vmesnik)</a><%}%><br></p>
                <% }%>
                <h3>Nakupovalni voziček</h3>
               
                <ul>
                    <% if (cart != null && !cart.getPostavka().isEmpty()) {%>
                    <% for (Postavka p : cart.getPostavka()) {%>
                    <li class="item">
                        <a href="#showEvent"><%= p.getVstopnica().getNaziv()%></a>
                        <span class="price"><%= p.getKolicina() + " x " + p.getVstopnica().getCena() + " EUR  "%>
                            <a href="index.jsp?bPostavko=<%=p.getId()%>" class="xBrisi">
                                <img src="blueprint/plugins/buttons/icons/cross.png" alt="Zbrisi"/></a>
                        </span>
                        
                    </li>
                    <% }%>
                    <li class="total">Skupaj: <%= cart.getVsotaCene()%> EUR</li>
                </ul>
                <a href="checkout.jsp">Zaključi z nakupom <img src="blueprint/plugins/buttons/icons/tick.png" alt="Zaključi"/></a>
                <% } else {%>
                <h4>Vaš nakupovalni voziček je prazen.</h4>
                <% }%>
            </div>
        </div>
    </body>
</html>